import io
import json

import requests

import http.client as httplib

# url = r'http://hadoop2x-04:50070/webhdfs/v1/RawData/user/xujiang/FirstClass.txt?user.name=xujiang&op=CREATE&overwrite=true'
#
# response = requests.put(url)
# print(response.status_code)
# Location = response.headers["Location"]
# print(Location)
import io


import requests

kwargs = {}
kwargs["user.name"] = "app"
kwargs["op"] = "CREATE"
kwargs["overwrite"] = False

requests_session = requests.api
# 上传文件
try:
    response = requests_session.request("put",
                                        #'http://hadoop2x-01:50070/webhdfs/v1/RawData/_base_obj_db3/upload/1/zt.siamjournal.20200107_000.db3',
                                        'http://hadoop2x-01:50070/webhdfs/v1/BaseData/upload_db3/upload/1/zt.siamjournal.20200107_000.db3',
                                        params=kwargs,
                                        timeout=30,
                                        allow_redirects=False)

    print(response.status_code)
    print(response.headers)
    with open(r"F:\db32\cnki\base_obj_meta_a.wf_qk.20200921_000.db3", 'rb') as f:
        print(type(f))
        response = requests_session.put(response.headers['location'], data=f)
        print(response.status_code)
        print(response.headers)
except Exception as e:
     # ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))
     # 这个可能是用户权限问题
     print("***********")
     print(e)
##################################################
# # 查看文件
# kwargs["op"] = "LISTSTATUS"
# response = requests_session.request("get",
#                                     'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/upload/4',
#                                     params=kwargs,
#                                     timeout=30,
#                                     allow_redirects=False)
# print(response.json())
# lists = []
# for item in response.json()["FileStatuses"]["FileStatus"]:
#     if item["pathSuffix"].endswith(".db3"):
#         lists.append(item["pathSuffix"])
# for db3name in lists:
#     kwargs["op"] = "RENAME"
#     kwargs["destination"] = "/RawData/_base_obj_db3/meta/{}".format(db3name)
#     response = requests_session.request("put",
#                                         'http://hadoop2x-04:50070/webhdfs/v1/RawData/_test/db3/{}'.format(db3name),
#                                         params=kwargs,
#                                         timeout=30,
#                                         allow_redirects=False)
#     print(response.json())

# 查看目录
# kwargs["op"] = "GETFILESTATUS"
# # response = requests_session.request("get",
# #                                     'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/upload/3',
# #                                     params=kwargs,
# #                                     timeout=30,
# #                                     allow_redirects=False)
# # if "RemoteException" in response.json():
# #     print("不存在目录,创建目录")
#     kwargs["op"] = "MKDIRS"
#     response = requests_session.request("put",
#                                         'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/upload/3',
#                                         params=kwargs,
#                                         timeout=30,
#                                         allow_redirects=False)
#     print(response.json())
#     if "boolean" in response.json() and response.json()["boolean"]:
#         print("创建目录成功")
#     else:
#         print("创建目录失败")
# else:
#     print("存在目录,删除目录里的文件")
#     kwargs["op"] = "DELETE"
#     kwargs["recursive"] = True
#     response = requests_session.request("delete",
#                                         'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/upload/3',
#                                         params=kwargs,
#                                         timeout=30,
#                                         allow_redirects=False)
#     print(response.json())
#     if "boolean" in response.json() and response.json()["boolean"]:
#         print("删除目录及里面的文件成功")
#     else:
#         print("删除目录及里面的文件失败")
#     kwargs["op"] = "MKDIRS"
#     response = requests_session.request("put",
#                                         'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/upload/2',
#                                         params=kwargs,
#                                         timeout=30,
#                                         allow_redirects=False)
#     if "boolean" in response.json() and response.json()["boolean"]:
#         print("创建目录成功")
#     else:
#         print("创建目录失败")

# print("存在目录,删除目录里的文件")
# kwargs["op"] = "DELETE"
# kwargs["recursive"] = True
# response = requests_session.request("delete",
#                                     'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/upload/1',
#                                     params=kwargs,
#                                     timeout=30,
#                                     allow_redirects=False)
# print(response.json())
#
# kwargs["op"] = "MKDIRS"
# kwargs["permission"] = "777"
# response = requests_session.request("put",
#                                     'http://hadoop2x-04:50070/webhdfs/v1/RawData/_base_obj_db3/upload/1',
#                                     params=kwargs,
#                                     timeout=30,
#                                     allow_redirects=False)
# if "boolean" in response.json() and response.json()["boolean"]:
#     print("创建目录成功")
# else:
#     print("创建目录失败")

# kwargs = {}
# kwargs["user.name"] = "qhy"
# kwargs["op"] = "MKDIRS"
# kwargs["permission"] = 775
#
# requests_session = requests.api
# # 创建目录
# response = requests_session.request("put",
#                                     'http://hadoop2x-04:50070/webhdfs/v1/BaseData/rule/once/1_25',
#                                     params=kwargs,
#                                     timeout=30,
#                                     allow_redirects=False)
# print(response.json())
#
# dicArgs = {
#     "logdicts": {
#         "file_type": "1",
#         "userid": 1,
#         "username": "1",
#         "ip": "1",
#         "ruleid": "1"
#     },
#     "rule_id": "1_25",
#     "extract_count": 0,
#     "rule_type": 0,
#     "rule": {"a": "中文", "c": "d"}
# }
#
# kwargs["op"] = "CREATE"
# kwargs["overwrite"] = False
# response = requests_session.request("put",
#                                     'http://hadoop2x-04:50070/webhdfs/v1/BaseData/rule/once/1_25/1_25.rule',
#                                     params=kwargs,
#                                     timeout=30,
#                                     allow_redirects=False)
# # 正常状态码 307
# if response.status_code == 307:
#     #response = requests_session.put(response.headers['location'], data=json.dumps(dicArgs,ensure_ascii=False).encode('utf-8'))
#     response = requests_session.put(response.headers['location'], data=dicArgs.encode('utf-8'))
#     # 第二次请求正常状态码 201
#     if response.status_code == 201:
#         print("true")
#     else:
#         print("false")